import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { ElMessage } from 'element-plus';

const Home = () => import('@/view/home/index.vue')
const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/home'
    },
    { path: '/home', component: Home },
    {
        path: '/goods',
        name: 'goods',
         meta: {
            title: '全部礼品'
          },
        component: () => import('../view/goods/index.vue')
    },
    {
        path: '/user',
        name: 'user',
        redirect: "/user/cart",
        meta: {
            title: '个人中心'
          },
        component: () => import('../view/user/index.vue'),
        children: [{
            path: "cart",
            name: "cart",
            component: () =>
                import ("../components/user/Cart.vue"),
        }],
        beforeEnter: (to, from, next) => {
            let token = localStorage.getItem("x-auth-token");
            if (token) {
                next();
            } else {
                ElMessage.error('请先登录！！')
            }
        }
    },
    {
        path: '/order',
        name: 'order',
        component: () => import('../view/order/index.vue')
    },
    {
        path: '/free',
        name: 'free',
        component: () => import('../view/free/index.vue')
    },
    {
        path: '/details',
        name: 'details',
        component: () => import('../view/details/index.vue')
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

export default router;